package com.sgb.kjwl.view.widget.manage

import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.text.TextUtils
import android.text.style.ImageSpan
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.databinding.DataBindingUtil
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.TempalteWebsiteOneBinding
import com.sgb.kjwl.databinding.TemplateWebsiteTwoBinding
import com.sgb.kjwl.model.entity.manage.WebsiteBannerEntity
import com.sgb.kjwl.model.entity.manage.WebsiteInfoEntity
import com.sgb.link.utils.SizeUtils
import com.sgb.kjwl.model.entity.manage.WebsiteMemberEntity
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity
import com.stx.xhb.androidx.XBanner
import com.stx.xhb.androidx.XBanner.XBannerAdapter
import com.swgk.core.util.ClickUtils

/**
 * 网站头部
 */
class WebsiteHeadView : RelativeLayout {

    companion object{
        const val TEMPLATE_TYPE_ONE = 1
        const val TEMPLATE_TYPE_TWO = 2
        const val TEMPLATE_TYPE_THREE = 3
    }


    private lateinit var mTemplateOne : TempalteWebsiteOneBinding

    private lateinit var mTemplateTwo : TemplateWebsiteTwoBinding

    private var mCallBack : ClickHeadView ?= null

    private var imageSpan : ImageSpan ?= null

    private var mTemplateType =  TEMPLATE_TYPE_ONE

    private var mWebsiteType = WebsiteInfoActivity.WEBSITE_FOR_COMPANY


    private var mContext: Context? = null
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int)
            : super(context, attrs, defStyleAttr) {
        this.mContext = context!!
        init()
    }

    fun setCallBack(mCallBack : ClickHeadView){
        this.mCallBack = mCallBack
    }

    fun init(){
        visibility = GONE
        setBackgroundColor(resources.getColor(R.color.color_white))
        removeAllViews()
    }


    fun loadTemplate(templateType : Int, websiteType : Int){
        this.mTemplateType = templateType
        this.mWebsiteType = websiteType
        when (templateType){
            TEMPLATE_TYPE_ONE -> {
                visibility = VISIBLE
                mTemplateOne = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.tempalte_website_one, this, true)
                setPadding(0, 0, 0, SizeUtils.dp2px(6f))
            }

            TEMPLATE_TYPE_TWO -> {
                visibility = VISIBLE
                mTemplateTwo = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.template_website_two, this, true)
                setPadding(0,0,0,0)
            }
            TEMPLATE_TYPE_THREE -> {
                visibility = GONE
            }
        }
    }


     fun renderView(entity: WebsiteInfoEntity, requestManager: RequestManager, mMemberEntity : WebsiteMemberEntity?) {
        if (mTemplateType == TEMPLATE_TYPE_ONE){
            mTemplateOne.entity = entity
            mTemplateOne.tvSelfContent.visibility = if (mWebsiteType == WebsiteInfoActivity.WEBSITE_FOR_COMPANY && entity.isSelfSupport) VISIBLE else GONE
            mTemplateOne.ivVr.visibility = if (TextUtils.isEmpty(entity.vrUrl)) GONE else VISIBLE
            if (mWebsiteType == 1){//企业
                mTemplateOne.ivAddress.visibility = if (TextUtils.isEmpty(entity.mapAddress)) GONE else VISIBLE
            } else if (mWebsiteType == 0){//个人
                mTemplateOne.ivAddress.visibility = if (TextUtils.isEmpty(entity.detailAddress)) GONE else VISIBLE
            } else if (mWebsiteType == 2){//团队
                mTemplateOne.ivAddress.visibility = if (TextUtils.isEmpty(entity.businessAddress)) GONE else VISIBLE
            } else if (mWebsiteType == 3){//组织
                mTemplateOne.ivAddress.visibility = if (TextUtils.isEmpty(entity.address)) GONE else VISIBLE
            }

            mTemplateOne.ivVideo.visibility = if (TextUtils.isEmpty(entity.videoUrl)) GONE else VISIBLE
            mTemplateOne.ivAddress.setOnClickListener {
                openMap(entity)
            }
            mTemplateOne.ivVr.setOnClickListener {
                openVr(entity.vrUrl)
            }
            mTemplateOne.ivVideo.setOnClickListener {
                openVideo(entity)
            }
            mTemplateOne.ivAttention.setOnClickListener {
                goToAttention()
            }
            var RIcon = R.mipmap.image_website_dfg
            if (mWebsiteType == WebsiteInfoActivity.WEBSITE_FOR_COMPANY) {
                RIcon = R.mipmap.image_website_dfg
            } else if (mWebsiteType == WebsiteInfoActivity.WEBSITE_FOR_PERSONAL) {
                RIcon = R.mipmap.def_photo
            } else if (mWebsiteType == WebsiteInfoActivity.WEBSITE_FOR_TEAM) {
                RIcon = R.drawable.icon_team_default
            } else if (mWebsiteType == WebsiteInfoActivity.WEBSITE_FOR_ORGANIZE) {
                RIcon = R.drawable.icon_organization_default
            }
            val options = RequestOptions()
                .placeholder(RIcon) //图片加载出来前，显示的图片
                .error(RIcon) //图片加载失败后，显示的图片
                .transform(CircleCrop())
            requestManager.load(entity.logo).apply(options).into(mTemplateOne.ivShopAvatar)
            if (mMemberEntity != null){
                if (!TextUtils.isEmpty(mMemberEntity.demandLevel) && !TextUtils.isEmpty(mMemberEntity.supplyLevel)){
                    mTemplateOne.tvMemberLevel.visibility = GONE
                } else if (TextUtils.isEmpty(mMemberEntity.demandLevel) && TextUtils.isEmpty(mMemberEntity.supplyLevel)){
                    mTemplateOne.tvMemberLevel.visibility = GONE
                } else {
                    mTemplateOne.tvMemberLevel.visibility = VISIBLE
                    mTemplateOne.tvMemberLevel.text = if (TextUtils.isEmpty(mMemberEntity.demandLevel)) mMemberEntity.supplyLevel else mMemberEntity.demandLevel
                }
            } else {
                mTemplateOne.tvMemberLevel.visibility = GONE
            }
            mTemplateOne.tvScore.visibility = if (mWebsiteType == WebsiteInfoActivity.WEBSITE_FOR_COMPANY) VISIBLE else GONE
            mTemplateOne.tvScore.text = resources.getString(R.string.website_score, if (TextUtils.isEmpty(entity.stringOutE)) "-" else "" + entity.stringOutE)
        } else if (mTemplateType == TEMPLATE_TYPE_TWO){
            mTemplateTwo.tvSelfContent.visibility = if (mWebsiteType == WebsiteInfoActivity.WEBSITE_FOR_COMPANY && entity.isSelfSupport) VISIBLE else GONE
            mTemplateTwo.ivVr.visibility = if (TextUtils.isEmpty(entity.vrUrl)) GONE else VISIBLE
            if (mWebsiteType == 1){ //
                mTemplateTwo.ivAddress.visibility = if (TextUtils.isEmpty(entity.mapAddress)) GONE else VISIBLE
            } else if (mWebsiteType == 0){
                mTemplateTwo.ivAddress.visibility = if (TextUtils.isEmpty(entity.detailAddress)) GONE else VISIBLE
            } else if (mWebsiteType == 2){
                mTemplateTwo.ivAddress.visibility = if (TextUtils.isEmpty(entity.businessAddress)) GONE else VISIBLE
            } else if (mWebsiteType == 3){
                mTemplateTwo.ivAddress.visibility = if (TextUtils.isEmpty(entity.address)) GONE else VISIBLE
            }
            mTemplateTwo.ivVideo.visibility = if (TextUtils.isEmpty(entity.videoUrl)) GONE else VISIBLE
            mTemplateTwo.ivAddress.setOnClickListener {
                if (!ClickUtils.isDoubleClick()){
                    openMap(entity)
                }
            }
            mTemplateTwo.ivVr.setOnClickListener {
                if (!ClickUtils.isDoubleClick()){
                    openVr(entity.vrUrl)
                }
            }
            mTemplateTwo.ivVideo.setOnClickListener {
                if (!ClickUtils.isDoubleClick()){
                    openVideo(entity)
                }
            }
            mTemplateTwo.ivAttention.setOnClickListener {
                if (!ClickUtils.isDoubleClick()){
                    goToAttention()
                }
            }
            if (mMemberEntity != null){
                if (!TextUtils.isEmpty(mMemberEntity.demandLevel) && !TextUtils.isEmpty(mMemberEntity.supplyLevel)){
                    mTemplateTwo.tvMemberLevel.visibility = GONE
                } else if (TextUtils.isEmpty(mMemberEntity.demandLevel) && TextUtils.isEmpty(mMemberEntity.supplyLevel)){
                    mTemplateTwo.tvMemberLevel.visibility = GONE
                } else {
                    mTemplateTwo.tvMemberLevel.visibility = VISIBLE
                    mTemplateTwo.tvMemberLevel.text = if (TextUtils.isEmpty(mMemberEntity.demandLevel)) mMemberEntity.supplyLevel else mMemberEntity.demandLevel
                }
            } else {
                mTemplateTwo.tvMemberLevel.visibility = GONE
            }
            mTemplateTwo.tvScore.visibility = if (mWebsiteType == WebsiteInfoActivity.WEBSITE_FOR_COMPANY) VISIBLE else GONE
            mTemplateTwo.tvScore.text = resources.getString(R.string.website_score, if (TextUtils.isEmpty(entity.stringOutE)) "-" else "" + entity.stringOutE)
        }
    }

    //关注
    private fun goToAttention(){
        if (mCallBack != null){
            mCallBack!!.clickAttention()
        }
    }

    //视频
    private fun openVideo(entity: WebsiteInfoEntity){
        if (mCallBack != null){
            mCallBack!!.clickVideo(entity)
        }
    }

    //vr
    private fun openVr(vrUrl : String){
        WebViewX5HostActivity.start(mContext, vrUrl)
        if (mCallBack != null){
            mCallBack!!.clickVr(vrUrl)
        }
    }

    //地图
    private fun openMap(entity: WebsiteInfoEntity){
        if (mCallBack != null){
            mCallBack!!.clickMap(entity)
        }
    }

    fun setHeaderBanners(bannerEntities: ArrayList<WebsiteBannerEntity>, websiteType : Int,
                         requestManager: RequestManager) {
        if (bannerEntities.isEmpty()) {
            bannerEntities.add(WebsiteBannerEntity())
        }
        var banner : XBanner?= null
        if (mTemplateType == TEMPLATE_TYPE_ONE){
            banner = mTemplateOne.bannerWebsite
        } else if (mTemplateType == TEMPLATE_TYPE_TWO) {
            banner = mTemplateTwo.bannerWebsite
        }
        if (banner == null) return
        banner.setBannerData(bannerEntities)
        banner.loadImage(XBannerAdapter { banner, model, view, position ->
            var errorImg = R.drawable.pic_default_bg
            if (websiteType == WebsiteInfoActivity.WEBSITE_FOR_COMPANY ) {
                errorImg = R.drawable.img_website_banner_company_default
            } else if (websiteType == WebsiteInfoActivity.WEBSITE_FOR_PERSONAL || websiteType == WebsiteInfoActivity.WEBSITE_FOR_ORGANIZE) {
                errorImg = R.drawable.img_website_banner_person_default
            } else if (websiteType == WebsiteInfoActivity.WEBSITE_FOR_TEAM) {
                errorImg = R.drawable.img_website_banner_team_default
            }
            requestManager.load(bannerEntities[position].xBannerUrl)
                .placeholder(R.drawable.pic_default_bg).error(errorImg).into(view as ImageView)
            view.scaleType = ImageView.ScaleType.FIT_XY
        })
        banner.setAutoPalyTime(2000)
        banner.setPointsIsVisible(false)
    }


    fun setAttention(flag: Boolean, isMyself: Boolean){
        if (mTemplateType == TEMPLATE_TYPE_ONE){
            if (isMyself) {
                mTemplateOne.ivAttention.visibility = GONE
            } else {
                mTemplateOne.ivAttention.visibility = VISIBLE
                if (!flag) {
//                    imageSpan = CenterImageSpan(
//                        mContext!!,
//                        R.drawable.icon_attention_heart
//                    )
//                    val spanString = SpannableString("关 关注")
//                    spanString.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//                    mTemplateOne.tvAttention.text = spanString
                    mTemplateOne.ivAttention.setImageResource(R.drawable.icon_not_attention)
                } else {
                    mTemplateOne.ivAttention.setImageResource(R.drawable.icon_attentioned)
//                    mTemplateOne.tvAttention.text = "已关注"
//                    mTemplateOne.tvAttention.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                }
            }
        } else if (mTemplateType == TEMPLATE_TYPE_TWO){
            if (isMyself) {
                mTemplateTwo.ivAttention.visibility = GONE
            } else {
                mTemplateTwo.ivAttention.visibility = VISIBLE
                if (!flag) {
//                    imageSpan = CenterImageSpan(
//                        mContext!!,
//                        R.drawable.icon_attention_heart
//                    )
//                    val spanString = SpannableString("关 关注")
//                    spanString.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
//                    mTemplateTwo.tvAttention.text = spanString
                    mTemplateTwo.ivAttention.setImageResource(R.drawable.icon_not_attention)
                } else {
                    mTemplateTwo.ivAttention.setImageResource(R.drawable.icon_attentioned)
//                    mTemplateTwo.tvAttention.text = "已关注"
//                    mTemplateTwo.tvAttention.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                }
            }
        }
    }


    interface ClickHeadView{
       fun clickVr(vrUrl : String)
       fun clickAttention()
       fun clickVideo(entity: WebsiteInfoEntity)
       fun clickMap(entity: WebsiteInfoEntity)
    }

}